package cn.htsyb.synergy.tester.frame;

import cn.htsyb.synergy.tester.config.DriverConfig;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

import java.util.concurrent.TimeUnit;

public abstract class WebDriverMgr {
    static WebDriver webDriver;
    static String mainWinHand;
    static Actions action;
    private static final Log defLog = LogFactory.getLog(WebDriverMgr.class);

    public static void removeDriver() {
        if (webDriver != null) {
            webDriver.close();
            webDriver.quit();
        }
        webDriver = null;
    }

    public static WebDriver getRunnerDriver(String browserType) {
        if (webDriver == null) {
            if (DriverConfig.getCompelBool() && StringUtils.isNotEmpty(browserType)) {
                browserType = DriverConfig.getBrowserType();
            }
            defLog.info("加载全局WebDriver驱动:" + browserType);
            switch (browserType.toLowerCase()) {
                /*case "firefox":
                    webDriver = buildFireFoxDriver();
                    break;*/
                case "chrome":
                    webDriver = buildChromeDriver();
                    break;
                /*case "ie":
                    webDriver = buildIEDriver();
                    break;
                default:
                    webDriver = buildIEDriver();
                    break;*/
            }
            webDriver.manage().timeouts().pageLoadTimeout(DriverConfig.getPageloadTimeOut(), TimeUnit.SECONDS);
            webDriver.manage().timeouts().setScriptTimeout(DriverConfig.getSetScriptTimeout(), TimeUnit.SECONDS);
        }
        mainWinHand = webDriver.getWindowHandle();
        action = new Actions(webDriver);
        webDriver.manage().window().maximize();
        return webDriver;
    }

    public static WebDriver buildChromeDriver() {
        String driverPath = DriverConfig.getDriverPathOfWebDriver("chrome");
        if (null != driverPath && driverPath.trim().length() > 0) {
            System.setProperty("webdriver.chrome.driver", driverPath);
        }
//        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
//
//        capabilities.setCapability("chrome.switches", "--incognito"); // 屏蔽--ignore-certificate-errors提示
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--test-type");// 忽略网站证书错误提示

//        options.merge(capabilities);
        return new ChromeDriver(options);

    }

    /*public static WebDriver buildIEDriver() {
        String driverPath = DriverConfig.getDriverPathOfWebDriver("ie");
        if (null != driverPath && driverPath.trim().length() > 0) {
            System.setProperty("webdriver.ie.driver", driverPath);
        }
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability("enablePersistentHover", true);
        capabilities.setCapability("ignoreZoomSetting", true);
        capabilities.setCapability("ignoreProtectedModeSetting", true);
        capabilities.setCapability("requireWindowFocus", true);
        InternetExplorerOptions options = new InternetExplorerOptions();
        options.merge(capabilities);
        return new InternetExplorerDriver(options);
    }

    public static WebDriver buildFireFoxDriver() {
        String binaryPath = DriverConfig.getBinaryPathOfWebDriver("firefox");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        if (null != binaryPath && binaryPath.trim().length() > 0) {
            System.setProperty("webdriver.firefox.bin", binaryPath);
            FirefoxOptions  options =new FirefoxOptions();
            options.merge(capabilities);
            return new FirefoxDriver(options);
        }
        String driverPath = DriverConfig.getDriverPathOfWebDriver("firefox");
        if (null != driverPath && driverPath.trim().length() > 0) {
            System.setProperty("webdriver.gecko.driver", driverPath);
            GeckoDriverService geckoDriverService= GeckoDriverService.createDefaultService();
            return new FirefoxDriver(geckoDriverService);
        }
        return null;
    }*/

    public WebDriver getRunnerDriver() {
        return getRunnerDriver(null);
    }
}
